class TeamController {
    constructor($rootScope, $scope, $cookieStore, $timeout, $state, ngDialog, dialogService, TeamService, getCourseList, iRunner){

    	var vm = this;

    	vm.$rootScope    = $rootScope;
    	vm.$cookieStore  = $cookieStore;
    	vm.$timeout 	 = $timeout;
    	vm.$state   	 = $state;
    	vm.ngDialog 	 = ngDialog;
    	vm.dialogService = dialogService;
        vm.TeamService   = TeamService;
    	vm.getCourseList = getCourseList;
    	vm.iRunner 	     = iRunner;

        // 团队性别
        vm.team_gender = iRunner.teamGender;

        // 科目列表
        vm.getCourseList.code==0&&(vm.courselist = vm.getCourseList.data);

        // 置空团队选手, 用以隐藏团队选手HTML
        vm.runnerlist = [];

        // 表单数据初始化
        vm.team = {
            team_name: '',
            team_gender: '',
            course: ''
        }

    }


    // 表单提交
    submitForm(isValid) {

        var vm = this;

        // 验证失败, 提示用户
        if (!isValid) {
            if (!vm.team.course) {
                $.blink(angular.element(document.getElementsByName('course')), 5, 100)
            } else if (!vm.team.team_name) {
                $.blink(angular.element(document.getElementsByName('teamName')), 5, 100)
            } else if (!vm.team.team_gender) {
                $.blink(angular.element(document.getElementsByName('teamGender')), 5, 100)
            }
            return false;
        }

        // 验证通过
        if (isValid) {

            // 拼接POST数据格式
            var params = {
                team_name: vm.team.team_name,
                team_gender: vm.team.team_gender.id,
                course_id: vm.team.course.course_id
            }

            console.log(params)

            // 新增团队
            vm.TeamService.postTeamCreate(params, vm.$cookieStore.get('access_token'), vm.$state.params.race_id)
            .then( data => {

                // 成功调用
                data.code==0&&vm.dialogService.success('创建成功', 'dashboard.race.teamlist');

                // 调用失败, 提示用户
                (data.code!=0&&data.code!=-1001)&&vm.dialogService.error(data.message);

                // 登录信息已过期, 请重新登录
                data.code==-1001&&vm.dialogService.error(data.message, true);

                console.log(data)
            }, error => console.log('error'));
        }

        return false;
    }

}

TeamController.$inject = ['$rootScope', '$scope', '$cookieStore', '$timeout', '$state', 'ngDialog', 'dialogService', 'TeamService', 'getCourseList', 'iRunner']; 

export default TeamController;